package cn.com.shadowless.xinning.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import com.mengpeng.mphelper.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.login.LoginInfo;
import cn.com.shadowless.xinning.presenter.LoginPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.utils.ProgressDialogUtils;
import cn.com.shadowless.xinning.view.BaseActivity;
import cn.jpush.android.api.JPushInterface;

/**
 * The type Main activity.
 *
 * @author sHadowLess
 */
public class LoginActivity extends BaseActivity {

    private final String TAG = LoginActivity.class.getSimpleName();
    private long justTime = 0;

    /**
     * The Login input user name.
     */
    @BindView(R.id.login_input_userName)
    EditText loginInputUserName;
    /**
     * The Login input pwd.
     */
    @BindView(R.id.login_input_Pwd)
    EditText loginInputPwd;
    /**
     * The Login btn.
     */
    @BindView(R.id.login_Btn)
    Button loginBtn;

    @Override
    protected int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void setData() {
        String userName = PreferencesUtils.getString(this, "userName", "-1");
        //initLogout(userName);
    }

    /**
     * On view clicked.
     */
    @OnClick(R.id.login_Btn)
    public void onViewClicked() {
        String userName = loginInputUserName.getText().toString().trim();
        String pwd = loginInputPwd.getText().toString().trim();
        String registrationId = JPushInterface.getRegistrationID(this);
        if (TextUtils.isEmpty(userName) && TextUtils.isEmpty(pwd)) {
            ToastUtils.onErrorShowToast("请输入用户名和密码");
        } else {
            ProgressDialogUtils.showProgressDialog(LoginActivity.this, "登录中...");
            LoginPresenter.getSingleton().getLoginInfo(userName, pwd, registrationId, new RequestCallBack<LoginInfo>() {
                @Override
                public void success(RequestResult<LoginInfo> result) {
                    if (null != result.getResult() && result.isSucceed()) {
                        Log.e(TAG, "success: " + result.toString());
                        boolean isUserSuccess = PreferencesUtils.putString(MyApplication.getInstance(), "userName", userName);
                        boolean isPasswordSuccess = PreferencesUtils.putString(MyApplication.getInstance(), "password", pwd);
                        boolean isStorage = PreferencesUtils.putString(MyApplication.getInstance(), "token", result.getLoginToken());
                        if (isUserSuccess && isPasswordSuccess && isStorage) {
                            Intent intent = new Intent(LoginActivity.this, OrderActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtils.onErrorShowToast("登陆状态无法保存，请联系管理员");
                        }
                    } else {
                        ToastUtils.onErrorShowToast("登录失败，请检查账号或密码");
                    }
                    ProgressDialogUtils.dismissProgressDialog();
                }

                @Override
                public void fail(String errorMsg) {
                    ProgressDialogUtils.dismissProgressDialog();
                    ToastUtils.onErrorShowToast(errorMsg);
                }
            });
        }
    }

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - justTime) > 2000) {
            ToastUtils.onInfoShowToast("再次点击退出应用");
            justTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }
    }
}